<?php
namespace Common\Model;
class SmsModel extends BaseModel {

    /**
     * 活动字段对应
     * 用户，user
     * 物品，item
     * 电话，phone
     * 地址，address
     * 时间，date
     */

    //短信模板
    const SMS_TPL_EVENT_SELECTED        = 'SMS_165414302';//活动入选（备用）
    const SMS_TPL_EVENT_SELECTED_COMPLEX= 'SMS_165414304';//活动入选通知，加参与条件-副本
    const SMS_TPL_EVENT_SELECTED_2      = 'SMS_172597658';//活动入选通知2
    const SMS_TPL_TOY_APPLY             = 'SMS_165419142';//您好，有宝妈对您的宝贝“${item}”发起了兑换申请，请尽快至兑换页进行确认，丸子妈妈！
    const SMS_TPL_TOY_SUCCESS_WITH_TEL  = 'SMS_165419143';//您的宝贝“${item}”匹配成功啦，请尽快联系${phone}，确认交换方式。丸子妈妈！



    const SMS_TPL_HOUSEWORK_CANCEL      = 'SMS_165676324';//取消
    const SMS_TPL_HOUSEWORK_CHANGE      = 'SMS_166866808';//修改时间
    const SMS_TPL_HOUSEWORK_COMPLETE    = 'SMS_165691029';//完成
    const SMS_TPL_HOUSEWORK_ASSIGN      = 'SMS_165691030';//派单


    /**
     * 短信后台发送
     * @return array
     */
    public static function getSmsTpl(){
        return array(
            self::SMS_TPL_EVENT_SELECTED=>'尊敬的用户，您已入选${name}, 丸子妈妈感谢您的支持！',
            self::SMS_TPL_EVENT_SELECTED_2=>'尊敬的用户，您已获得${item}请于${condition}联系丸子妈妈，逾期视为弃权,丸子妈妈感谢您的支持！',
            self::SMS_TPL_EVENT_SELECTED_COMPLEX=>'尊敬的用户，您已入选${name},请凭${condition}到${address},丸子妈妈感谢您的支持！',
            //self::SMS_TPL_TOY_APPLY=>'您好，有宝妈对您的宝贝“${item}”发起了兑换申请，请尽快至兑换页进行确认，丸子妈妈！',
            //self::SMS_TPL_TOY_SUCCESS_WITH_TEL=>'您的宝贝“${item}”匹配成功啦，请尽快联系${phone}，确认交换方式。丸子妈妈！',
        );
    }



    const SMSCODEMSG = '验证码';

    public function SMS_Notce($phone,$tplCode,$param){
        $type = SEND_SMS_SMSTYPE;
        include_once BASE_PATH.'/soeLib/sms/'.$type.".api.php";
        $obj = new $type();
       
        return $obj->send($phone,$tplCode, $param);
    }

    //发送验证码
    public function SMS_Code($phone,$code){
        $type = SEND_SMS_SMSTYPE;
        include_once BASE_PATH.'/soeLib/sms/'.$type.".api.php";
        $obj = new $type();
        return $obj->sendCode($phone, $code);
    }


    /**
     * 发送短信
     * phone 手机号码 必填 多个用,分割
     * content 发送内容 必填
     * Author: RickSun
     *
     * return array('status'=>true|false,'msg'=>'发送成功')
     */
    public static function sendSms($phone,$tplCode, $param=array()){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

        M('t_sms_log', null, DB_MAIN_CFG)->add( array('addTime'=>time(),'phone'=>$phone,'content'=>json_encode($param),'type'=>2) );
        $phoneArr = explode(',',$phone);
        if(is_array($phoneArr)){
            foreach ( $phoneArr as $val){
                $res = self::SMS_Notce($val,$tplCode,$param);
                if($res!==0){
                    $dataFailure['msg'] =$val.'发送失败。原因：'.$res;
                    return $dataFailure;
                }
            }
        }else{
            $res = self::SMS_Notce($phone,$tplCode,$param);
            if($res!==0){
                $dataFailure['msg'] =$phone.'发送失败。原因：'.$res;
                return $dataFailure;
            }
        }
        $dataSuccess['msg'] = '发送成功';
        return $dataSuccess;
    }

    /**
     * 发送验证短信
     * phone 手机号码 必填 一个
     * bind 1 绑定 | 0 找回密码
     * Author: RickSun
     *
     * return array('status'=>true|false,'msg'=>'发送成功','code'=>$code)
     */
    public static function sendSmsCode($phone, $bindFlag, $uid = 0){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if($bindFlag){
            //绑定短信  手机号码不存在直接 换绑|绑定, 手机号存在且微信账号不存, 不可绑定|换绑
            if($uid) {
                $phoneRow = M('u_user_base', null, DB_MAIN_CFG)->where(" bindPhone = '{$phone}' ")->find();
                $rowWechat = array();
                if($phoneRow){
                    $rowWechat = M('u_user_weixin', null, DB_MAIN_CFG)->where(" uid='{$phoneRow['uid']}' ")->find();
                }

                $uinfo = M('u_user_base', null, DB_MAIN_CFG)->where(" uid = '{$uid}' ")->find();
                if($uinfo['bindPhone']){//换绑
                    if($phone == $uinfo['bindPhone']){
                        $dataFailure['msg'] = '该号码已经当前绑定手机！';
                        return $dataFailure;
                    }
                    if($phoneRow && !$rowWechat){
                        $dataFailure['msg'] = '该号码已经绑定过！';
                        return $dataFailure;
                    }
                }else{//绑定

                }

            }
        }else{
            //用于找回密码
            $row = M('u_user_base', null, DB_MAIN_CFG)->where(" bindPhone = '{$phone}' ")->find();
            if(!$row){
                $dataFailure['msg'] = '该手机号码不存在！';
                return $dataFailure;
            }
        }
        $code = mt_rand(1000,9999);
        $content = $code;

        $smsLogM = M('t_sms_log', null, DB_MAIN_CFG);
        $smsLogM->where("phone = '{$phone}' AND type = 1 AND status = 1")->save( array('status'=>3));//发短信之前把 之前未验证的短信过期处理

        $res = self::SMS_Code($phone,$content);
        if($res != 0){
            $dataFailure['msg'] = '发送失败，请稍后再试！';
            return $dataFailure;
        }

        $smsLogM->add( array('addTime'=>time(),'phone'=>$phone,'content'=>$content,'type'=>1,'code'=>$code,'status'=>1) );

        $dataSuccess['msg'] = '验证码发送成功！请注意查收';
        //$this->dataSuccess['code'] = $code;
        return $dataSuccess;
    }

    /**
     * 获取发送短信验证 code
     * phone 手机号码 必填
     * Author: RickSun
     *
     * return array('status'=>true|false,'code'=>$code)
     */
    public function getCode($phone){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        $row = M('t_sms_log', null, DB_MAIN_CFG)->where("phone = '{$phone}' AND type = 1 AND status = 1")->find();
        if(!$row){
            return $dataFailure;
        }
        $dataSuccess['code'] = $row['code'];
        return $dataSuccess;
    }



}